package com.king.gkrpc;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 表示服务
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ServiceDescript {
    private String clazz;
    private String method;
    private String returnType;
    private String[] paramTypes;

    public static ServiceDescript from(Class clazz, Method method){
        ServiceDescript sdp = new ServiceDescript();
        sdp.setClazz(clazz.getName());
        sdp.setMethod(method.getName());
        sdp.setReturnType(method.getReturnType().getName());
        Class[] parameterClasses = method.getParameterTypes();
        String[] paramTypes = new String[parameterClasses.length];
        for(int i=0;i<parameterClasses.length;i++){
            paramTypes[i] = parameterClasses[i].getName();
        }
        sdp.setParamTypes(paramTypes);
        return sdp;
    }

    @Override
    public boolean equals(Object o) {
        if(this == o){
            return true;
        }

        if(null == o || getClass() != o.getClass()) return false;

        ServiceDescript that = (ServiceDescript)o;

        return this.toString().equals(that.toString());
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    @Override
    public String toString() {
        return "ServiceDescript{" +
                "clazz='" + clazz + '\'' +
                ", method='" + method + '\'' +
                ", returnType='" + returnType + '\'' +
                ", paramTypes=" + Arrays.toString(paramTypes) +
                '}';
    }
}
